'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: 6_faiss_with_filter.py
* @Time: 2025/10/27
* @All Rights Reserve By Brtc
'''
import dotenv

from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings

dotenv.load_dotenv()
embedding = OpenAIEmbeddings(model="text-embedding-3-small")

texts = [ "笨笨是一只很喜欢睡觉的猫咪",
    "我喜欢在夜晚听音乐，这让我感到放松。",
    "猫咪在窗台上打盹，看起来非常可爱。",
    "学习新技能是每个人都应该追求的目标。",
    "我最喜欢的食物是意大利面，尤其是番茄酱的那种。",
    "昨晚我做了一个奇怪的梦，梦见自己在太空飞行。",
    "我的手机突然关机了，让我有些焦虑。",
    "阅读是我每天都会做的事情，我觉得很充实。",
    "他们一起计划了一次周末的野餐，希望天气能好。",
    "我的狗喜欢追逐球，看起来非常开心。",
]

metadatas=[
    {"page":1},
    {"page":2},
    {"page":3},
    {"page":4},
    {"page":5},
    {"page":6},
    {"page":7},
    {"page":8},
    {"page":9},
    {"page":10},
]
"""
db = FAISS.from_texts(texts=texts,
                      metadatas=metadatas,
                      embedding=embedding,
                      relevance_score_fn=lambda distance:1.0/(1.0 + distance))
print("Total Data:",db.index.ntotal)
search_list = db.similarity_search_with_relevance_scores("我养了一只猫叫做笨笨",filter = lambda x:x["page"]>5)

for item in search_list:
    print(item)


#删除
db.delete([db.index_to_docstore_id[0]])
print("After Delete Total Data:",db.index.ntotal)
#增加
db.add_texts(["博小睿已经7岁了"])
print("After Add Total Data:",db.index.ntotal)
# 存储到本地
db.save_local("./vector-store/")
"""
# 本地加载
db = FAISS.load_local("./vector-store/", embedding, allow_dangerous_deserialization=True)
search_list = db.similarity_search_with_relevance_scores("我养了一只猫叫做笨笨")
for item in search_list:
    print(item)
